package com.hdaccp.xingmu.service;


import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class HouseuserService {

    @PersistenceContext
    private EntityManager entityManager;

    public Map<String,Object> list(Map<String,Object> map){

        String jpal = "select h.id,h.doornumber,t.`name` as tname,s.`name` as sname,d.`name` as dname,h.floor,x.`name` as huxing,i.`name` as chaoxiang,h.measure,h.remark,(select u.`name` from userdetails as u where u.doornumber = h.`doornumber`) as `user` from house as h\n" +
                "INNER JOIN typeb as t on h.type = t.id\n" +
                "INNER JOIN sale as s on h.sale = s.id\n" +
                "INNER JOIN danyuan as d on h.danyuan = d.id\n" +
                "INNER JOIN huxing as X on h.huxing = x.id\n" +
                "INNER JOIN direction as i on h.direction = i.id where 1=1";
        if(map.get("shu")!=null && map.get("shu")!="undefined"){

            String t = (String) map.get("shu");
            String tt = t.substring(2,3);

            if(tt.equals("楼")){
                jpal+=" and s.name ="+map.get("shu");
            }else if(tt.equals("元")){
                jpal+=" and d.name ="+map.get("shu");
            }

            if(!map.get("type").equals("-1")){
                jpal+=" and t.id = "+map.get("type");
            }

            if(!map.get("number").equals("-1")){
                jpal+=" and h.doornumber = "+map.get("number");
            }
        }else{
           // int type = (int) map.get("type");
            if(!map.get("type").equals("-1")){
                jpal+=" and t.id = "+map.get("type");
            }

            if(!map.get("number").equals("-1")){
                jpal+=" and h.doornumber = "+map.get("number");
            }
        }
        System.out.println(jpal);
        Query qu=entityManager.createQuery(jpal);
        //起始页书
        qu.setFirstResult((int)map.get("qi"));
        //结束页数
        qu.setMaxResults((int)map.get("shi"));

        //查询多少条数据
        String jpa="select count(*) from House h where 1=1";

        if(map.get("shu")!=null && map.get("shu")!="undefined"){

            String t = (String) map.get("shu");
            String tt = t.substring(2,3);

            if(tt.equals("楼")){
                jpa+=" and s.name ="+map.get("shu");
            }else if(tt.equals("元")){
                jpa+=" and d.name ="+map.get("shu");
            }

            if(!map.get("type").equals("-1")){
                jpa+=" and t.id = "+map.get("type");
            }

            if(!map.get("number").equals("-1")){
                jpa+=" and h.doornumber = "+map.get("number");
            }
        }else{

            if(!map.get("type").equals("-1")){
                jpa+=" and t.id = "+map.get("type");
            }
            String number = (String) map.get("number");
            if(!map.get("number").equals("-1")){
                jpa+=" and h.doornumber = "+map.get("number");
            }
        }

        Long count=(Long) entityManager.createQuery(jpa).getSingleResult();

        Map<String,Object> map1=new HashMap<String,Object>();
        map1.put("total",count);
        map1.put("rows",qu.getResultList());
        return map1;
    }



}

